﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@ page import="com.util.SuperPage" %>
<%@ page import="my.myorm.tool.jdbc.SQLExecutor" %>
<%@ page import="my.myorm.tool.jdbc.ResultList" %>
<%@ page import="my.myorm.tool.jdbc.Pages" %>
<%@ page import="my.myorm.tool.jdbc.SQLManager" %>
<%@ page import="com.util.SuperString" %>
<%@ page import="com.mtbu.comm.Util" %>
<%@ page import="com.util.SuperDate" %>
<%@ page import="java.util.Calendar" %>
<%@page import="com.mtbu.bean.*"%>
<%@page import="com.mtbu.dao.*"%>
<%@page import="com.mtbu.service.DataUtil"%>
<%@page import="com.util.SuperFile"%>
<%@page import="com.util.SuperMath"%>
<%@page import="java.util.Date"%>
<%@page import="com.util.DESUtil"%>
<%@page import="com.mtbu.comm.Constant"%>
<%@page import="com.mtbu.comm.PageCache"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.HashMap"%>

<%@include file="channel_list_comm.jsp"%>

<%!
	public String createStepHref(HttpServletRequest request,boolean isBackStep,Project project,Channel channel,SysUser user) throws Exception {

		try{
			Step step = channel.getStep();
			if ( isBackStep && step.getPreStep().getType() == 2 )
			{
				return "回退";
			}

			if ( !isBackStep && step.getNextStep() == null )
			{
				return "完成";
			}

			String curFile = SuperPage.getCurURLFile(request);
			String modFileDo = SuperFile.getFileName(curFile) + "moddo.jsp";

			//判断用户权限
			if ( user.isOwnerOfStep(step.getId()) ) {
				if( step.getType() == 0){
					return String.format(
						"<a href=\"%1$s?act=%2$s&ids=%3$s%4$s\">%5$s</a>" ,
						"channelflowmoddo.jsp",
						isBackStep ? "stepback" : "stepnext" ,
						channel.getId(),
						SuperPage.getQueryAnd(request,"act,ids"),
						isBackStep ? "回退" : step.getName()
					);
				}
				else if(step.getType() == 1){
					return String.format(
						"<a href=\"%1$s?act=%2$s&ids=%3$s%4$s\">%5$s</a>" ,
						"channelflowmoddo.jsp",
						isBackStep ? "stepback" : "stepnext" ,
						channel.getId(),
						SuperPage.getQueryAnd(request,"act,ids"),
						isBackStep ? "回退" : step.getName()
					);
				}
				else{
					return "异常";
				}
			}
			else {
				return isBackStep ? "回退" : step.getName();
			}
		}
		catch(Exception e){
			e.printStackTrace();
			return "状态异常";
		}
	}


	public String createStepDelChannelHref(HttpServletRequest request,boolean isAllowDel ,Channel channel){
		if ( isAllowDel == false )
		{
			return "删除";
		}
		else{
			return String.format(
				"<a href=\"%1$s?act=%2$s&ids=%3$s%4$s\">%5$s</a>" ,
				"channelflowmoddo.jsp",
				"del",
				channel.getId(),
				SuperPage.getQueryAnd(request,"act,ids"),
				"删除"
			);
		}
	}
%>

<%
    String curFile = SuperPage.getCurURLFile(request);
    String curName = "频道列表";
	String pid=SuperString.notNullTrim(request.getParameter("pid"));
	Project project = DAOManager.projectDao.findProject(pid);
	boolean isAllowDel = project.isAllowDelChannel(Constant_User);

	String tblSplit= project.getSplit();
	String modFile = "XML_channel_mod.jsp";
    int s_id=SuperString.getInt(request.getParameter("s_id"),-1);
    int s_pagesize=SuperString.getInt(request.getParameter("s_pagesize"));
    //用于记录显示的分集
    int setpid = SuperString.getInt(request.getParameter("setpid"),0);
    int setlength = SuperString.getInt(request.getParameter("setlength"),0);



    String dropparam="s_id,s_pagesize,issearch";
    if(request.getMethod().equalsIgnoreCase("post")){
        StringBuffer sbparam=new StringBuffer();
        sbparam.append("&issearch=1");
        if(s_id>-1) sbparam.append("&s_id=").append(s_id);
        if(s_pagesize>0) sbparam.append("&s_pagesize=").append(s_pagesize);
        if(setpid>0) sbparam.append("&setpid=").append(setpid);
        if(setlength>0) sbparam.append("&setlength=").append(setlength);
        String strparam=sbparam.toString();
        if(strparam.startsWith("&")) strparam="?"+strparam.substring(1);
        strparam+="".equals(strparam)? SuperPage.getQueryQ(request,dropparam): SuperPage.getQueryAnd(request,dropparam);
        response.sendRedirect(curFile+strparam);
        return;
    }
    //组合条件
    StringBuffer strw = new StringBuffer();
    //strw.append(" where status>=0");
    if(s_id>-1) strw.append(" and channelid=").append(s_id);

    String order = SuperString.notNullTrim(request.getParameter("order"));
    String orderby = order.length() == 0 ? " order by id desc" :
            " order by " + order;

    SQLExecutor dbconn = SQLManager.getSQLExecutor();
    SQLExecutor epgconn = SQLManager.getSQLExecutor("EPGDS");
    String sql="select * from channel" + tblSplit + " b "+ Util.getWhereSQL(strw) + orderby;
    int pagesize=Util.getPageSizeOfCookie(request,response,s_pagesize);
    Pages pages = new Pages(pagesize, "id", request);

    ResultList rs = dbconn.query(sql,true,pages);

    StringBuffer ids = new StringBuffer();
    ArrayList<Channel> list = new ArrayList<Channel>();
	//System.out.println("rs.size():" + rs.size());
    for(int i = 0; i < rs.size(); i++) {
    	Channel wc = rs.get(i).get(Channel.class);

    	if(i==0) ids.append("(").append(wc.getChannelId());
    	else ids.append(",").append(wc.getChannelId());
    	list.add(wc);
    }
	ids.append(")");
    HashMap<Integer,BppChannel> bppmap = new HashMap<Integer,BppChannel>();
    if(rs.size()>0){
    	sql = "select ID,Title,EpTitle,VT,RID,CataID,PID,GID,Duration,Bitrate,Status,Rank,PicUrl,VideoLanguage,VideoType,OpLog,filename,infoid from bpp_channel where id in "+ids.toString();
    	ResultList bpp_rs = epgconn.query(sql,false,null);
    	for(int i=0;i<bpp_rs.size();i++){
    		BppChannel bc = bpp_rs.get(i).get(BppChannel.class);
    		bppmap.put(bc.getId(),bc);
    	}
    }
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><%=curName%></title>
<link href="css/adminPPLive.css" rel="stylesheet" type="text/css" />
<link href="css/popcore.css" type="text/css" rel="stylesheet"/>
<script language="javascript" src="js/comm.js"></script>
<script language="javascript" src="js/events.js"></script>
<script language="javascript" src="js/eventutil.js"></script>
<script language="javascript" src="js/tablecell.js"></script>
<script type="text/javascript" src="js/popup_layer.js" language="javascript"></script>
<script type="text/javascript" src="js/jquery.js" language="javascript"></script>
<script type="text/javascript" src="js/jquery.alerts.js" language="javascript"></script>
<script type="text/javascript" src="js/tooltips.js" language="javascript"></script>
<script type="text/javascript" src="js/popup_layer.js" language="javascript"></script>
<script type="text/javascript" src="js/jquery.js" language="javascript"></script>
<script type="text/javascript" src="js/jquery.alerts.js" language="javascript"></script>
<script type="text/javascript" src="js/tooltips.js" language="javascript"></script>
<script type="text/javascript" src="js/commselect_catalog.js" language="javascript"></script>


<script language="javascript" src="js/calendar.js"></script>
</head>
<body>
<div id="toolTipLayer" style="position:absolute; visibility: hidden"></div>
<script language="JavaScript">initToolTips();</script>
<div class="adminRight">
  <div class="adminBorderTop">
    <div class="adminBorderTopLeft"></div>
    <div class="adminBorderTopTitle">
      <div class="adminBorderTopTitleLeft"></div>
      <div class="adminBorderTopTitleMiddle">频道列表</div>
      <div class="adminBorderTopTitleRight"></div>
    </div>
    <div class="adminBorderTopRight"></div>
  </div>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#F7F8F9">
  <tr>
    <td class="adminBorderMiddleLeft"></td>
    <td>

    <div class="adminWel" id="admBppChannel">
      <div class="adminPrompt">
        <div class="adminPromptTilte"><span>输入查询条件</span></div>
        <div class="adminPromptContent">
        <form name="form_s" action="<%=curFile%><%=SuperPage.getQueryQ(request,dropparam)%>" method="post" class="formline">
    	<table width="100%"  border="0" cellspacing="1" cellpadding="2" style="margin:5px 0;">
        <tr>
          <td width="91%">
            频道ID
            <input name="s_id" type="text" id="s_id" title="ID" value="<%=s_id>-1?s_id:""%>" size="8" maxlength="30" class="admTextComm">
            每页显示
            <input name="s_pagesize" type="text" id="s_pagesize" title="每页显示" value="<%=pagesize%>" size="2" maxlength="6" class="admTextComm">
            <input name="Submit2" type="submit" value="" style="background-image:url(images/adminSearchBtn.png);width: 59;height: 22;border:0px;cursor:pointer;">
            <br/>
            </td>
        </tr>
          </table></form>
        </div>
      </div>
    </div>

<!--分页开始-->
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr valign="middle">
    <td width="100%" colspan="2" align="left" bgcolor="#e7f0f9"><div class="admPage"><%=SuperPage.generatePageNavSingleNew(rs.getAllCount(), request, null, pages.getPageSize(),"个")%></div></td>
  </tr>

    <tr>
		<td width="80%" bgcolor="#e7f0f9" align="left">
			<div class="admHouseBtn">
				<li id="bc_del_down">
				   <a href="javascript:void(0)" onclick="batchDel('<%=project.getName()%>')">批量移除<%=project.getName()%></a>
				</li>
			</div>
		</td>
		<td width="20%" align="right" bgcolor="#e7f0f9">页内查找:<input type="text" id="filterPageIpt" onkeyup="filtePage()" class="admTextSix"/></td>
	</tr>


</table>
<!--分页结束-->

<table id="table_data" width="100%" border="0" cellpadding="1" cellspacing="1" class="table_bord">
  <tr class="table_bord_ttl">
    <td nowrap width="10"><input name="ckall" type="checkbox" class="checkbox"  onClick="javascript:allselect(this)"></td>
    <td nowrap width="40"onClick="title_click('<%=Util.getOrderUrl("channelid",order,request)%>')" >频道ID<%=Util.getOrderByImgHTML("channelid",order)%></td>
    <td nowrap>频道名</td>
    <td nowrap>分集名</td>
    <td nowrap width="65">频道类型</td>
    <td nowrap width="65">基本分类</td>
    <td nowrap width="50" onClick="title_click('<%=Util.getOrderUrl("status",order,request)%>')">Bpp状态</td>
	<!-- <td nowrap width="50" onClick="title_click('<%=Util.getOrderUrl("status",order,request)%>')">Pub状态<%=Util.getOrderByImgHTML("status",order)%></td> -->

    <!-- <td nowrap width="30">权重</td> -->
	<td nowrap width="30">PID</td>
    <td nowrap width="40">语言</td>
    <td nowrap width="60">类型</td>
    <td nowrap width="120">添加时间</td>
    <td nowrap width="120">操作</td>
  </tr>
<%
    for(int i = 0; i < list.size(); i++) {
    	Channel record= list.get(i);
    	BppChannel bc = bppmap.get(record.getChannelId());
    	if(bc==null) continue;
        String trclass = i % 2 == 0 ? "tr_class1" : "tr_class2";
%>
  <tr name="datatr" id="tr_channel_<%=record.getId()%>" class="<%=trclass%>" onMouseMove="cell_over(this);" onMouseOut="cell_out(this)" onClick="cell_click(this);" height="20">
    <td nowrap class="font22"><input id="ids_<%=record.getId()%>" name="check_id" type="checkbox" value="<%=record.getId() %>"  class="checkbox"></td>
    <td><%=bc.getId()%></td>
    <td><%=bc.getTitle()%></td>
    <td><%=bc.getEpTitle()%></td>
    <td><span style="float: left;"><%= bc.getPID() == 0 ? DataUtil.getVT(bc.getVT()) : DataUtil.getVT(bc.getVT()) + "(剧集)" %></span></td>
    <td><%=DataUtil.getBppCataTitleWithParent(bc.getCataID(),false)%></td>
    <td ><font color="<%=bc.getStatus()<_statuscolors.length?_statuscolors[bc.getStatus()]:""%>"><%=DataUtil.getStatus(bc.getStatus())%></font></td>

    <!-- <td><%=bc.getRank()%></td> -->
	<td><%=bc.getPID()%></td>
	<td><font style="font-size: 12px;"><%=DataUtil.getVideoLanguage(bc.getVideoLanguage())%></font></td>
    <td><font style="font-size: 12px;"><%=DataUtil.getVideoType(bc.getVideoType())%></font></td>
    <td><%=record.getCreateTime_ToDateTimeString()%></td>
    <td>
		<!-- 后退操作 -->
		<%= createStepHref(request,true,project,record,Constant_User)%>
		<!-- 当前step操作 -->
		<%= createStepHref(request,false,project,record,Constant_User) %>
		<!-- 特殊 type=2的那个step -->
		<%= createStepDelChannelHref(request,isAllowDel,record) %>

    </td>
  </tr>
<%
    }
    if(rs.size()==0){
%>
  <tr bgcolor="#FFFFFF">
    <td height="50" colspan="20" align="center">没有数据</td>
  </tr>
  <%
    }
%>
</table>
<!--分页开始-->
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="80%" bgcolor="#e7f0f9" align="left">
		<div class="admHouseBtn">
			<li id="bc_del_down">
			   <a href="javascript:void(0)" onclick="batchDel('<%=project.getName()%>')">批量移除<%=project.getName()%></a>
			</li>
		</div>
	</td>
  </tr>
  <tr valign="middle">
    <td width="100%" align="left" bgcolor="#e7f0f9"><div class="admPage"><%=SuperPage.generatePageNavSingleNew(rs.getAllCount(), request, null, pages.getPageSize(),"个")%></div></td>
  </tr>
</table>
   </td>
    <td class="adminBorderMiddleRight"></td>
  </tr>
</table>
<div class="adminBorderBottom">
  <div class="adminBorderBottomLeft"></div>
  <div class="adminBorderBottomRight"></div>
</div>
</div>
<script type="text/javascript">
var selall=false;
function allselect(e){
    $("input[name='check_id']").attr("checked",$(e).attr("checked"));
    $("input[name='ckall']").attr("checked",$(e).attr("checked"));
}
function dbClickTR(id){
	if($('#ids_'+id).attr('checked')) $('#ids_'+id).attr('checked',false);
	else $('#ids_'+id).attr('checked',true);
}
function batchDel(name){
    var ids="";
    var checks=document.getElementsByName("check_id");
    if(checks==null||checks==undefined) return;
    for(var i=0;i<checks.length;i++){
        if(checks[i].checked){
           ids+=checks[i].value+",";
        }
    }
    if(ids==''){
        jError('<font color=\"red\">请选择要移除的频道!</font>', '移除' + name + '节目', function(r){});
        return;
    }
	del(ids,name);
}

function batchPub(split,name){
    var ids="";
    var checks=document.getElementsByName("check_id");
    if(checks==null||checks==undefined) return;
    for(var i=0;i<checks.length;i++){
        if(checks[i].checked){
           ids+=checks[i].value+",";
        }
    }
    if(ids==''){
        jError('<font color=\"red\">请选择要发布的频道!</font>', '发布频道', function(r){});
        return;
    }

	if(split == "up"){
		pubUp(ids,name);
	}
	else if(split == "down"){
		pubDown(ids,name);
	}
    else{
		jError('<font color=\"red\">name == null!</font>', 'Error', function(r){});
	}
}



function del(id,name){
    jAlarm('请确认您是否要将选该移除?', '移除频道', function(r) {
		if(r){
			$("#sp_act_up").html("正在处理...");
	        $.getJSON("<%=modFile%>", {ids:id,pt:name,pid:<%= project.getId() %>, act:"bcdel",time:new Date().getTime()}, function(data){
	        	$("#sp_act_up").html('<a href="javascript:void(0)">移除频道</a>');
				if(data['result']==0){
		        	 var result = '<font color=\"red\">发生错误!</font>移除频道操作<font color=\"red\">失败</font>!';
		        	 jError(result, '移除频道', function(r) {
	        			if(r){
	        				location.reload(true);
	        			}
        	    	});
	        	}else{
        			location.reload(true);
	        	}
	       	});
		}
	});
}
function pubUp(id,name){
    jAlarm('请确认您是否要发布' + name + '节目?', '发布' + name  + '节目', function(r) {
		if(r){
			$("#sp_act_up").html("正在处理...");
	        $.getJSON("<%=modFile%>", {ids:id,pt:name, act:"bcpubup",time:new Date().getTime()}, function(data){
	        	$("#sp_act_up").html('<a href="javascript:void(0)">移除频道</a>');
				if(data['result']==0){
		        	 var result = '<font color=\"red\">发生错误!</font>发布' + name + '节目操作<font color=\"red\">失败</font>!';
		        	 jError(result, '发布' + name  + '节目', function(r) {
	        			if(r){
	        				location.reload(true);
	        			}
        	    	});
	        	}else{
        			location.reload(true);
	        	}
	       	});
		}
	});
}
function pubDown(id,name){
    jAlarm('请确认您是否要发布下线' + name + '节目?', '发布下线' + name  + '节目', function(r) {
		if(r){
			$("#sp_act_up").html("正在处理...");
	        $.getJSON("<%=modFile%>", {ids:id,pt:name, act:"bcpubdown",time:new Date().getTime()}, function(data){
	        	$("#sp_act_up").html('<a href="javascript:void(0)">发布下线频道</a>');
				if(data['result']==0){
		        	 var result = '<font color=\"red\">发生错误!</font>发布下线' + name + '节目操作<font color=\"red\">失败</font>!';
		        	 jError(result, '发布下线' + name  + '节目', function(r) {
	        			if(r){
	        				location.reload(true);
	        			}
        	    	});
	        	}else{
        			location.reload(true);
	        	}
	       	});
		}
	});
}

</script>
<%@include file="sys_commfooter.jsp"%>
</body>
</html>